package com.baoxin.utils.dao;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.beans.PropertyDescriptor;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author: Rose
 * @Date: 2021/3/23 19:06
 * @version: 1.0.0
 */
public class JpaUtil {


    /**
     *
     *
     * @param src
     * @param target
     * @param Field 排除指定字段
     */
    public static  void copyNotNullPropertiesExclude(Object src, Object target, String[] Field){
        BeanUtils.copyProperties(src,target,getNullPropertyNames(src,Field,true));
    }

    /**
     *
     * @param src
     * @param target
     * @param excludeField  允许指定字段
     */
    public static void  copyNotNullPropertiesAllow(Object src, Object target,String[] excludeField){
        BeanUtils.copyProperties(src,target,getNullPropertyNames(src,excludeField,false));
    }

    public static  void copyPropertiesAllow(Object src ,Object target, String[] excludeField) {
        BeanUtils.copyProperties(src,target,getPropertyNames(src,excludeField,false));
    }

    /**
     *
     *
     * @param source
     * @param excludeField
     * @param tag
     * @return
     */
    private static String[] getPropertyNames(Object source, String[] excludeField, boolean tag) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        PropertyDescriptor[] descriptors = src.getPropertyDescriptors();
        Set<String> emptyNames = new HashSet<>();
        for(PropertyDescriptor pd : descriptors){
            if(pd.getName().equals("baseData")){
                continue;
            }else
            {
                Object propertyValue = src.getPropertyValue(pd.getName());
                // pd.getName() 存在 Allow, Exclude
                boolean contains = Arrays.asList(excludeField).contains(pd.getName());
                if(contains == tag){
                    emptyNames.add(pd.getName());
                }
            }
        }

        String[] result = new String[emptyNames.size()];

        return emptyNames.toArray(result) ;
    }

    /**
     *
     *
     *
     * @param source
     * @param excludeField
     * @param tag  筛选忽略字段
     * @return
     */
    public static String[] getNullPropertyNames(Object source, String[] excludeField, boolean tag) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        PropertyDescriptor[] descriptors = src.getPropertyDescriptors();
        Set<String> emptyNames = new HashSet<String>();
        for(PropertyDescriptor pd : descriptors){
            if(pd.getName().equals("baseData")){
                continue;
            }else {
                Object srcValue = src.getPropertyValue(pd.getName());
                // pd.getName() 存在Allow. Exclude
                boolean contains = Arrays.asList(excludeField).contains(pd.getName());
                if(srcValue == null || contains == tag){
                    emptyNames.add(pd.getName());
                }

            }
        }
        String[]  result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }


}
